package com.woniuxy.factory.controller;

import com.woniuxy.factory.entity.MaterialRequisition;
import com.woniuxy.factory.entity.ResponseResult;
import com.woniuxy.factory.entity.WorkOrder;
import com.woniuxy.factory.entity.WorkShop;
import com.woniuxy.factory.service.MaterielAppService;
import com.woniuxy.factory.service.WorkOrderService;
import com.woniuxy.factory.service.WorkShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class MaterielAppController {

    @Autowired
    MaterielAppService mas;
    @Autowired
    WorkShopService ws;
    @Autowired
    WorkOrderService wos;

    //查询用料申请信息表
    @ResponseBody
    @RequestMapping("/materielapply")
    public ResponseResult selectApplyInfo(MaterialRequisition materialRequisition,int page,int limit){
        //调用service层方法查询信息表，并返回一个list集合
        List<MaterialRequisition> lmr =  mas.selectApplyInfo(materialRequisition,page,limit);
        //查询申请信息总的数目
        int count = mas.selectApplyInfoSum(materialRequisition);
        //创建响应对象，将响应码，消息，总条数，查询得到的list集合赋值给响应对象
        ResponseResult rr = new ResponseResult(0,"",count,lmr);
        return rr;
    }

    //查询车间信息
    @ResponseBody
    @RequestMapping("/allworkshop")
    public ResponseResult selectFactory(){
        List<WorkShop> lws = ws.findWorkShop();
        ResponseResult rr = new ResponseResult(0,"",0,lws);
        return rr;
    }

    //申请添加回显派单信息
    @RequestMapping("/applyinfoecho")
    @ResponseBody
    public ResponseResult applyInfoEcho(String planid){
        //根据当前派单计划编号，查询派单信息
        WorkOrder wo = wos.selectWorkOrderEchoById(planid);
        ResponseResult rr = new ResponseResult(0,"",0,wo);
        return rr;
    }

    //申请添加
    @RequestMapping("/addapply")
    @ResponseBody
    public ResponseResult addApply(MaterialRequisition mr){
        mas.addApply(mr);
        ResponseResult rr = new ResponseResult(0,"添加成功",0,null);
        return rr;
    }

    //申请信息编辑数据回显
    @ResponseBody
    @RequestMapping("/applydataecho")
    public Map<String,Object> selectApplyAndFactory(String matId){
        //定义map集合存储当前申请信息对象，以及所有的车间信息集合
        Map<String,Object> m = new HashMap<String,Object>();
        //查询车间
        List<WorkShop> lws = ws.findWorkShop();
       //根据当前申请编号查询申请信息
        MaterialRequisition lmr = mas.selectApplyByid(matId);
        //存入map集合
        m.put("workShop",lws);
        m.put("materialRequisition",lmr);
        return m;
    }

    //修改申请
    @RequestMapping("/updateapply")
    @ResponseBody
    public ResponseResult updateApply(MaterialRequisition mr){
        mas.updateApply(mr);
        ResponseResult rr = new ResponseResult(0,"修改成功",0,null);
        return rr;
    }

    //删除申请
    @RequestMapping("/deleteapply")
    @ResponseBody
    public ResponseResult deleteApply(String matId){
        if(matId==null){
            return null;
        }
        mas.deleteApply(matId);
        ResponseResult rr = new ResponseResult(0,"删除成功",0,null);
        return rr;
    }
}
